//
//  TMTVInfoPlayView.swift
//  MoviesInfo
//
//  Created by zhangkai on 2023/5/2.
//  Copyright © 2023 Tobias Ruano. All rights reserved.
//

import UIKit
import BMPlayer

class TMTVInfoPlayView: UIViewController {
    
    var player:BMPlayer!

    override func viewDidLoad() {
        super.viewDidLoad()
        
//        setupPlayerManager()
        preparePlayer()
//        setupPlayerResource()

        // Do any additional setup after loading the view.
        
        
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(applicationDidEnterBackground),
                                               name: UIApplication.didEnterBackgroundNotification,
                                               object: nil)
        
        NotificationCenter.default.addObserver(self,
                                               selector: #selector(applicationWillEnterForeground),
                                               name: UIApplication.willEnterForegroundNotification,
                                               object: nil)
    }
    

    @objc func applicationWillEnterForeground() {
      
    }
    
    @objc func applicationDidEnterBackground() {
      player.pause(allowAutoPlay: false)
    }
    
    
    func preparePlayer() {
        let controller: BMPlayerControlView? = nil
            
      player = BMPlayer(customControlView: controller)
      view.addSubview(player)
        player.frame = CGRectMake(0, 0, UIScreen.main.bounds.size.width, 211);

      
      player.delegate = self
      player.backBlock = { [unowned self] (isFullScreen) in
        if isFullScreen {
          return
        } else {
          let _ = self.navigationController?.popViewController(animated: true)
        }
      }
      
      self.view.layoutIfNeeded()
    }
    
    @objc func setupPlayerResource(playstr:String) {
   
        let url = URL(string: playstr)!
     
        let asset = BMPlayerResource(name: "Video Name Here",
                                     definitions: [BMPlayerResourceDefinition(url: url, definition: "480p")],
                                     cover: nil,
                                     subtitles: nil)
        player.setVideo(resource: asset)
        player.playTimeDidChange = { time,time1 in
              
              print("第一个\(time)")
       
              
          }

    }
    
    override func viewWillDisappear(_ animated: Bool) {
      super.viewWillDisappear(animated)
      UIApplication.shared.setStatusBarStyle(UIStatusBarStyle.default, animated: false)
      // If use the slide to back, remember to call this method
      // 使用手势返回的时候，调用下面方法
      player.pause(allowAutoPlay: true)
    }
    
    
    override func viewWillAppear(_ animated: Bool) {
      super.viewWillAppear(animated)
      UIApplication.shared.setStatusBarStyle(UIStatusBarStyle.lightContent, animated: false)
      // If use the slide to back, remember to call this method
      // 使用手势返回的时候，调用下面方法
      player.autoPlay()
    }
    
    deinit {
      // If use the slide to back, remember to call this method
      // 使用手势返回的时候，调用下面方法手动销毁
      player.prepareToDealloc()
      print("VideoPlayViewController Deinit")
    }

    

}


extension TMTVInfoPlayView: BMPlayerDelegate {
  // Call when player orinet changed
  func bmPlayer(player: BMPlayer, playerOrientChanged isFullscreen: Bool) {
    player.snp.remakeConstraints { (make) in
      make.top.equalTo(view.snp.top)
      make.left.equalTo(view.snp.left)
      make.right.equalTo(view.snp.right)
      if isFullscreen {
        make.bottom.equalTo(view.snp.bottom)
      } else {
        make.height.equalTo(view.snp.width).multipliedBy(9.0/16.0).priority(500)
      }
    }
  }
  
  // Call back when playing state changed, use to detect is playing or not
  func bmPlayer(player: BMPlayer, playerIsPlaying playing: Bool) {
    print("| BMPlayerDelegate | playerIsPlaying | playing - \(playing)")
  }
  
  // Call back when playing state changed, use to detect specefic state like buffering, bufferfinished
  func bmPlayer(player: BMPlayer, playerStateDidChange state: BMPlayerState) {
    print("| BMPlayerDelegate | playerStateDidChange | state - \(state)")
  }
  
  // Call back when play time change
  func bmPlayer(player: BMPlayer, playTimeDidChange currentTime: TimeInterval, totalTime: TimeInterval) {
    //        print("| BMPlayerDelegate | playTimeDidChange | \(currentTime) of \(totalTime)")
  }
  
  // Call back when the video loaded duration changed
  func bmPlayer(player: BMPlayer, loadedTimeDidChange loadedDuration: TimeInterval, totalDuration: TimeInterval) {
    //        print("| BMPlayerDelegate | loadedTimeDidChange | \(loadedDuration) of \(totalDuration)")
  }
}
